<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Database Operations</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        .container {
            max-width: 600px;
            margin: auto;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
        }
        input[type="text"], input[type="number"] {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
        }
        button {
            padding: 10px 15px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }
        button:hover {
            background-color: #0056b3;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Database Operations</h1>
        <div class="form-group">
            <label for="itemName">Item Name:</label>
            <input type="text" id="itemName" required>
        </div>
        <div class="form-group">
            <label for="value">Value:</label>
            <input type="text" id="value" required>
        </div>
        <button onclick="insertData()">Insert Data</button>

        <div class="form-group">
            <label for="updateId">Update ID:</label>
            <input type="number" id="updateId">
        </div>
        <div class="form-group">
            <label for="updateValue">New Value:</label>
            <input type="text" id="updateValue">
        </div>
        <button onclick="updateData()">Update Data</button>

        <div class="form-group">
            <label for="deleteId">Delete ID:</label>
            <input type="number" id="deleteId">
        </div>
        <button onclick="deleteData()">Delete Data</button>

        <button onclick="fetchData()">Fetch Data</button>

        <h2>Database Records</h2>
        <table>
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Item Name</th>
                    <th>Value</th>
                </tr>
            </thead>
            <tbody id="data-table">
                <!-- Data will be populated here -->
            </tbody>
        </table>
    </div>

    <script>
        function insertData() {
            const itemName = document.getElementById('itemName').value;
            const value = document.getElementById('value').value;

            fetch('/api/insert', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ itemName, value })
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                fetchData();  // Refresh the data
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }

        function updateData() {
            const id = document.getElementById('updateId').value;
            const value = document.getElementById('updateValue').value;

            fetch(`/api/update/${id}`, {
                method: 'PUT',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ value })
            })
            .then(response=> response.json())
            .then(data => {
                alert(data.message);
                fetchData();  // Refresh the data
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }

        function deleteData() {
            const id = document.getElementById('deleteId').value;

            fetch(`/api/delete/${id}`, {
                method: 'DELETE'
            })
            .then(response => response.json())
            .then(data => {
                alert(data.message);
                fetchData();  // Refresh the data
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }

        function fetchData() {
            fetch('/api/data')
            .then(response => response.json())
            .then(data => {
                const dataTable = document.getElementById('data-table');
                dataTable.innerHTML = '';
                data.forEach(record => {
                    const row = `<tr>
                                    <td>${record.id}</td>
                                    <td>${record.itemName}</td>
                                    <td>${record.value}</td>
                                </tr>`;
                    dataTable.insertAdjacentHTML('beforeend', row);
                });
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }
    </script>
</body>
</html>
